package com.fone.player.client;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.fone.player.ApplicationManage;

import com.fone.player.util.Base64;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.StringUtil;




public class Reporter {
	
	static private StringBuffer replocalInf = new StringBuffer();
	//static private StringBuffer replocaInfTmp  = new StringBuffer();
	static private StringBuffer playrepInf = new StringBuffer();
	//static private StringBuffer playrepInfTmp = new StringBuffer();
	
	static private Class_d h5_evnt;
	
	static private Class_play player_event;
	
	
	public static class ErrorType {
		
		public static final int CACHE_ERROR = 1;         /* 缓存失败 */
		public static final int CACHE_PLAY_ERROR = 2;    /* 缓存播放失败 */
		public static final int ONLLINE_PLAY_ERROR = 3;  /* 在线播放失败 */
		public static final int SYNC_COLLECT_ERROR = 4;  /* 收藏至云端失败 */
		public static final int LOGIN_ERROR = 5;  		 /* 登陆空间失败 */
        public static final int LAUNCH_PLAYER_ERROR = 6; /* 打开播放器失败 */

	}
	
	public static class EventId {
		
		/*通知上报*/
		public static final int NOTIC_INNER_CLICK = 0x9000000b; /*内通知点击次数*/
		public static final int NOTIC_OUTER_CLICK = 0x9000000d; /*外通知点击次数*/

		/*升级上报 */
		public static final int UPGRADE_RECEIVE      = 0x90000003; /*收到升级提示框的次数*/
		public static final int UPGRADE_CLICK_OK     = 0x90000005; /*点击“确定”升级次数*/
		public static final int UPGRADE_CLICK_CANCEL = 0x90000007; /*点击“取消”升级次数*/
		public static final int UPGRADE_DOWNLOAD     = 0x90000009; /*成功下载升级包的次数*/
		
		public static final int UPGRADE_POPUP        = 0x90000017; /* 收到弹窗用户数*/
		public static final int UPGRADE_SELF_CHECK   = 0x90000019; /* 自检升级成功*/
		
		/*用户按钮点击次数上报*/
		
		public static final int CLICK_TOTAL_BTN       = 0xa0000011; /*‘全部’按钮*/
		public static final int QUICK_PLAY_ENTRY  = 0xa0000013; /*快速播放入口*/
		public static final int CLICK_MY_VEDIO_BTN    = 0xa0000015; /*我的视频入口次数*/
		public static final int CLICK_NOTIC_BTN       = 0xa0000017; /*“通知”入口*/
		public static final int CLICK_MSG_BTN         = 0xa0000019; /*“消息”入口*/
		public static final int CLICK_RECHARGE_BTN    = 0xa000001B; /*“马上充值”按钮*/
		public static final int CLICK_VIP_BTN         = 0xa000001D; /*来自“我的”点击“成为 VIP”*/
		public static final int CLICK_VIP_CHANNEL_BTN = 0xa000001F; /*来自 VIP 频道的“成为 VIP”*/
		public static final int CLICK_MY_COLLECT_BTN  = 0xa0000021; /*“我的收藏”入口*/
		public static final int CLICK_MY_CACHE_BTN    = 0xa0000023; /*“我的缓存”入口*/
		public static final int CLICK_LOCAL_BTN       = 0xa0000025; /*本地入口*/
		public static final int CLICK_SEARCH_BTN      = 0xa0000027; /*搜索入口*/
		public static final int CLICK_DETAIL_SHARE_BTN = 0xa0000029; /*详情页的分享按钮*/
		public static final int CLICK_DETAIL_BTN       = 0xa000002B; /*详情页的评论按钮*/
		public static final int CLICK_DETAIL_BOOK_BTN  = 0xa000002D; /*详情页的预约按钮*/
		public static final int CLICK_H5_PLAY_BTN      = 0xa000002F; /*页面的 100TV 播放按钮*/
		public static final int CLICK_CLASS_BTN        = 0xa0000031; /*分类检索按钮*/
		public static final int POPUP_VIP              = 0xa0000033; /*是否成为VIP弹框次数*/
		public static final int DNLD_MORE_BTN          = 0xa0000035; /*确认按钮点击次数*/
		public static final int POPUP_VIP_CANCEL       = 0xa0000037; /*取消按钮点击次数*/
		public static final int CLICK_VIDEO_DETAIL_COLLECT_BTN = 0xa0000039; /*详情页收藏按钮点击次数*/
		public static final int CLICK_QUICK_PLAY_BTN       = 0xa000003A; /*快速播放按钮点击次数*/

		
		/*编辑频道上报*/
		public static final int EDIT_CHANNEL           = 0xb0000000;
		
		/*操作失败上报接口*/
		public static final int OPERATION_FAIL         = 0xc0000000; 
		
		/*H5页面播放日志上报*/
		public static final int H5_PLAY         	   = 0xd0000000; 
		
		/*访问分享日志上报*/
		public static final int SHARE_LOG         	   = 0xe0000000; 
		
		/*点击权利页面“加入vip”上报数据*/
		public static final int JOIN_VIP         	   = 0xf0000000; 
	}
	
	static private class Class_d {
		public String entry_time; /*停留开始时间*/
		public String exit_time;  /*停留结束时间*/
		public String cid;        /*内容id*/
		public String ccid;       /*栏目id*/
		public int comeFrom;      /*来源*/
		
		public String toReportString() {
			StringBuffer str = new StringBuffer();
			str.append("d");

			str.append(entry_time);
			str.append(exit_time);
			
			int len = 9 - cid.length();
			while(len > 0) {
				str.append(' ');
				len--;
			}
			str.append(cid);
			
			len = 9 - ccid.length();
			while(len > 0) {
				str.append(' ');
				len--;
			}
			str.append(ccid);

			str.append(String.format("%02d", comeFrom));
			
			len = 60 - str.length();

			while (len-- > 0) {
				str.append(' ');
			}

			return str.toString();
		}
		
	}
	
	static private class Class_a {
		
		static public byte click_total_btn = 0;
		static public byte quick_play_entry = 0;
		static public byte click_my_vedio_btn = 0;
		static public byte click_notic_btn = 0;
		static public byte click_msg_btn = 0;
		static public byte click_recharge_btn = 0;
		static public byte click_vip_btn = 0;
		static public byte click_vip_channel_btn = 0;
		static public byte click_my_collect_btn = 0;
		static public byte click_my_cache_btn = 0;
		static public byte click_local_btn = 0;
		static public byte click_search_btn = 0;
		static public byte click_detail_share_btn = 0;
		static public byte click_detail_btn = 0;
		static public byte click_detail_book_btn = 0;
		static public byte click_h5_play_btn = 0;
		static public byte click_class_btn = 0;
		static public byte popup_vip = 0;
		static public byte dl_more_btn = 0;
		static public byte popup_vip_cancel  = 0;
		static public byte click_video_detail_cillect_btn = 0;
		static public byte click_quick_play_btn = 0;
		
		
		static public String toReportString() {
			StringBuffer str = new StringBuffer();
			str.append("a");
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");       
        	Date curDate = new Date(System.currentTimeMillis());
        	
        	str.append(formatter.format(curDate));
			
			str.append(String.format("%02d", click_total_btn));
			str.append(String.format("%02d", quick_play_entry));
			str.append(String.format("%02d", click_my_vedio_btn));
			str.append(String.format("%02d", click_notic_btn));
			str.append(String.format("%02d", click_msg_btn));
			str.append(String.format("%02d", click_recharge_btn));
			str.append(String.format("%02d", click_vip_btn));
			str.append(String.format("%02d", click_vip_channel_btn));
			str.append(String.format("%02d", click_my_collect_btn));
			str.append(String.format("%02d", click_my_cache_btn));
			str.append(String.format("%02d", click_local_btn));
			str.append(String.format("%02d", click_search_btn));
			str.append(String.format("%02d", click_detail_share_btn));
			str.append(String.format("%02d", click_detail_btn));
			str.append(String.format("%02d", click_detail_book_btn));
			str.append(String.format("%02d", click_h5_play_btn));
			str.append(String.format("%02d", click_class_btn));
			str.append(String.format("%02d", popup_vip));
			str.append(String.format("%02d", dl_more_btn));
			str.append(String.format("%02d", popup_vip_cancel));
			str.append(String.format("%02d", click_video_detail_cillect_btn));
			str.append(String.format("%02d", click_quick_play_btn));
			
			int len = 60 - str.length();

			while (len-- > 0) {
				str.append(' ');
			}
			
			return str.toString();
		}
		
		public static void save() {
			setPrefString("Class_a", toReportString());
		}
	}
	
	static private class Class_9 {
		
		static public byte upgrade_receive = 0;
		static public byte upgrade_click_ok = 0;
		static public byte upgrade_click_cancel = 0;
		static public byte upgrade_download = 0;
		static public byte notice_inner_click = 0;
		static public byte notice_outer_click = 0;
		
		static public byte field_15 = 0;
		static public byte field_17 = 0;
		static public byte field_19 = 0;
		static public byte field_21 = 0;
		
		static public byte upgrad_popup = 0;
		static public byte upgrad_self_check = 0;
		
		
		static public String toReportString() {
			StringBuffer str = new StringBuffer();
			str.append("9");
			str.append(String.format("%02d", upgrade_receive));
			str.append(String.format("%02d", upgrade_click_ok));
			str.append(String.format("%02d", upgrade_click_cancel));
			str.append(String.format("%02d", upgrade_download));
			str.append(String.format("%02d", notice_inner_click));
			str.append(String.format("%02d", notice_outer_click));
			str.append(String.format("%02d", field_15));
			str.append(String.format("%02d", field_17));
			str.append(String.format("%02d", field_19));
			str.append(String.format("%02d", field_21));
			str.append(String.format("%02d", upgrad_popup));
			str.append(String.format("%02d", upgrad_self_check));
			
			int len = 60 - str.length();

			while (len-- > 0) {
				str.append(' ');
			}
	
			return str.toString();
		}
		
		public static void save() {
			setPrefString("Class_9", toReportString());
		}
	}
	
	static private class Class_play {
		public String start_time;
		public String end_time;
		public byte comefrom;
		public String net_type;
		public byte server_comefrom;
		public byte pause_times;
		
		public String cid;
		public String clid;
		public String suffix;
		public byte localSC;
		public String querydata; 
		
		public String toReportString() {
			StringBuffer str = new StringBuffer();
			str.append('0');
			str.append(start_time);
			str.append(end_time);
			str.append(String.format("%02d", comefrom));
			
			int len = 4;
			if (!StringUtil.isEmpty(suffix)) {
				len -= suffix.length();
					
			} 
			while (len > 0) {
				str.append(' ');
				len--;
			}
			if (suffix != null)
				str.append(suffix);
			
			
			str.append(net_type);
			str.append(String.format("%02d", server_comefrom));
			str.append(String.format("%02d", pause_times));
			
			len = 10;
			if (!StringUtil.isEmpty(cid)) {
				len -= cid.length();
					
			} 
			while (len > 0) {
				str.append(' ');
				len--;
			}
			if (cid != null)
				str.append(cid);
			
			len = 9;
			if (!StringUtil.isEmpty(clid)) {
				len -= clid.length();
				
			} 
			while (len > 0) {
				str.append(' ');
				len--;
			}
			if (clid != null)
				str.append(clid);
			
			str.append(String.format("%01d", localSC));
			
			if (querydata != null) {
				str.append(querydata);
			}
			
			writeLog("Class_play: toReportString" + str.toString());
			
			return str.toString();
			
		}
	
	}

	
	public static void logEvent (int eventId) {
		
		Log.v("Reporter", "logEvent: " + Integer.toHexString(eventId));
		
		writeLog("logEvent: " + Integer.toHexString(eventId));
		
		switch (eventId) {
	    	case EventId.NOTIC_INNER_CLICK      :
	    		Class_9.notice_inner_click++;
	    		Class_9.save();
	    		break;
	    		
			case EventId.NOTIC_OUTER_CLICK      :
				Class_9.notice_outer_click++;
				Class_9.save();
				break;
                                  
			case EventId.UPGRADE_RECEIVE        :
				Class_9.upgrade_receive++;
				Class_9.save();
				break;
				
			case EventId.UPGRADE_CLICK_OK       :
				Class_9.upgrade_click_ok++;
				Class_9.save();
				break;
				
			case EventId.UPGRADE_CLICK_CANCEL   :
				Class_9.upgrade_click_cancel++;
				Class_9.save();
				break;
				
			case EventId.UPGRADE_DOWNLOAD       :
				Class_9.upgrade_download = 1;
				Class_9.save();
				break;
	                                         
			case EventId.UPGRADE_POPUP          :
				Class_9.upgrad_popup++;
				Class_9.save();
				break;
				
			case EventId.UPGRADE_SELF_CHECK     :
				Class_9.upgrad_self_check = 1;
				Class_9.save();
				break;
                                   
			case EventId.CLICK_TOTAL_BTN        :
				Class_a.click_total_btn++;
				Class_a.save();
				break;
				
			case EventId.QUICK_PLAY_ENTRY   :
				Class_a.quick_play_entry++;
				Class_a.save();
				break;
				
			case EventId.CLICK_MY_VEDIO_BTN     :
				Class_a.click_my_vedio_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_NOTIC_BTN        :
				Class_a.click_notic_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_MSG_BTN          :
				Class_a.click_msg_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_RECHARGE_BTN     :
				Class_a.click_recharge_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_VIP_BTN          :
				Class_a.click_vip_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_VIP_CHANNEL_BTN  :
				Class_a.click_vip_channel_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_MY_COLLECT_BTN   :
				Class_a.click_my_collect_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_MY_CACHE_BTN     :
				Class_a.click_my_cache_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_LOCAL_BTN        :
				Class_a.click_local_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_SEARCH_BTN       :
				Class_a.click_search_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_DETAIL_SHARE_BTN :
				Class_a.click_detail_share_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_DETAIL_BTN      :
				Class_a.click_detail_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_DETAIL_BOOK_BTN  :
				Class_a.click_detail_book_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_H5_PLAY_BTN      :
				Class_a.click_h5_play_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_CLASS_BTN        :
				Class_a.click_class_btn++;
				Class_a.save();
				break;
				
			case EventId.POPUP_VIP                :
				Class_a.popup_vip++;
				Class_a.save();
				break;
				
			case EventId.DNLD_MORE_BTN        :
				Class_a.dl_more_btn++;
				Class_a.save();
				break;
				
			case EventId.POPUP_VIP_CANCEL         :
				Class_a.popup_vip_cancel++;
				Class_a.save();
				break;
				
			case EventId.CLICK_VIDEO_DETAIL_COLLECT_BTN :
				Class_a.click_video_detail_cillect_btn++;
				Class_a.save();
				break;
				
			case EventId.CLICK_QUICK_PLAY_BTN           :
				Class_a.click_quick_play_btn++;
				Class_a.save();
				break;
	
		}
	
		return;
		
	}
	
	public static void logError(String cid, int errorType) {
		StringBuffer inf = new StringBuffer();
		inf.append('c');
		int len = 9 - cid.length();
		while(len > 0) {
			inf.append(' ');
			len--;
		}
		inf.append(cid);
		inf.append(String.format("%01d", errorType));
		
		len = 60 - inf.length();

		while (len-- > 0) {
			inf.append(' ');
		}
		
		saveReplocal(inf.toString());

	}
	
	public static void logEditChannel(String cid, int addCount, int delCount) {
		StringBuffer inf = new StringBuffer();
		inf.append('b');
		int len = 9 - cid.length();
		while(len > 0) {
			inf.append(' ');
			len--;
		}
		inf.append(cid);
		inf.append(String.format("%02d", addCount));
		inf.append(String.format("%02d", delCount));
		
		len = 60 - inf.length();

		while (len-- > 0) {
			inf.append(' ');
		}
		
		saveReplocal(inf.toString());

	}
	
	public static void logLocalPlay(int from) {
		
		L.v("Reporter", "logLocalPlay " + from);
		
		writeLog("logLocalPlay " + from);
		
		StringBuffer inf = new StringBuffer();
		inf.append('1');
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");       
    	Date curDate = new Date(System.currentTimeMillis());
    	inf.append(formatter.format(curDate));
    	inf.append(getNetworkType());
    	inf.append(from);
    	
    	int len = 60 - inf.length();

		while (len-- > 0) {
			inf.append(' ');
		}
		
		saveReplocal(inf.toString());

	}
	
	private static String getNetworkType() {

		ConnectivityManager connectionManager = (ConnectivityManager) ApplicationManage.getGlobalContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();

		if (networkInfo == null) {
			return "0";
		} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

			TelephonyManager telephonyManager = (TelephonyManager) ApplicationManage.getGlobalContext()
					.getSystemService(Context.TELEPHONY_SERVICE);

			if (telephonyManager.getNetworkType() < 4) {
				return "3";
			} else if (telephonyManager.getNetworkType() == 13) {
				return "4";
			}else {
				return "2";
			}

		} else {
			return "1";
		}

	}
	
	public static void logShare(String cid, int state, int comeFrom) {
		
		L.v("Reporter", "logShare", " cid : "+ cid +" state : " + state +" comeFrom : " + comeFrom);
		
		writeLog("logShare: " + " cid : "+ cid +" state : " + state +" comeFrom : " + comeFrom);
		
		StringBuffer inf = new StringBuffer();
		inf.append('e');
		int len = 9 - cid.length();
		while(len > 0) {
			inf.append(' ');
			len--;
		}
		inf.append(cid);
		inf.append(String.format("%01d", state));
		inf.append(String.format("%01d", comeFrom));
		
		len = 60 - inf.length();

		while (len-- > 0) {
			inf.append(' ');
		}
		
		saveReplocal(inf.toString());
	
	}
	
	public static void logVipPage(String cid) {
		
		L.v("Reporter", "logVipPage", " cid : "+ cid);
		writeLog( "logVipPage " + " cid : "+ cid);

		StringBuffer inf = new StringBuffer();
		inf.append('f');
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");       
    	Date curDate = new Date(System.currentTimeMillis());
    	inf.append(formatter.format(curDate));
    	int len = 9 - cid.length();
		while(len > 0) {
			inf.append(' ');
			len--;
		}
		inf.append(cid);
		
		len = 60 - inf.length();

		while (len-- > 0) {
			inf.append(' ');
		}
		
		saveReplocal(inf.toString());
	}
	
	public static void logPlayerOpen(String url, byte comefrom) {
		
		L.v("Reporter", "logPlayerEnter", " url : "+ url + " comefrom "  +  comefrom);
		writeLog( "logPlayerEnter " + " url : "+ url + " comefrom "  +  comefrom);
		
		player_event = new Class_play();
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");       
    	Date curDate = new Date(System.currentTimeMillis());
    	
    	player_event.start_time = formatter.format(curDate);
    	player_event.comefrom = processFrom(comefrom);
    	player_event.localSC = 2;
    	
    	if (!StringUtil.isEmpty(url)) {
    		player_event.cid = FoneUtil.getCIdByUrl(url);
    		player_event.clid = FoneUtil.getCLIdByUrl(url);
    		player_event.querydata = FoneUtil.getUerydataByUrl(url);
    	}
    	
    	player_event.net_type = getNetworkType();

	}
	
	
	private static byte processFrom(byte from) {
		byte ret = 4;
		switch (from) {
		case 0:
			ret = 2;
			break;
		case 1:
			ret = 1; 
			break;
		case 2:
			ret = 23;
			break;
		case 3:
			ret = 4;
			break;
		case 4:
			ret = 4;
			break;
		case 5:
			ret = 27;
			break;
		case 6:
			ret = 4;
			break;
		case 7:
			ret = 4;
			break;
		case 8:
			ret = 4;
			break;
		case 9:
			ret = 27;
			break;
		case 10:
			ret = 27;
			break;
		case 11:
			ret = 4;
			break;
		case 12:
			ret = 18;
			break;
		case 13:
			ret = 4;
			break;
		case 40:
			ret = 18;
			break;
		case 46:
			ret = 27;
			break;
		case 47:
			ret = 28;
			break;
		case 48:
			ret = 29;
			break;
		case 49:
			ret = 26;
			break;
		case 50:
			ret = 26;
			break;
		case 51:
			ret = 25;
			break;
		case 52:
			ret = 26;
			break;
		case 53:
			ret = 31;
			break;
		case 54:
			ret = 32;
			break;
		case 55:
			ret = 23;
			break;
		case 56:
			ret = 12;
			break;
		case 57:
			ret = 30;
			break;
		case 58:
			ret = 33;
			break;
		case 59:
			ret = 34;
			break;

		}

		return ret;
	}

	
	public static void logPlayerClose() {
		
		L.v("Reporter", "logPlayerExit");
		
		writeLog("logPlayerExit " + player_event);
		
		if (player_event != null) {

			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
			Date curDate = new Date(System.currentTimeMillis());

			player_event.end_time = formatter.format(curDate);

			savePlayrep(player_event.toReportString());
			
			player_event = null;
		}

	}
	
	public static void logPlayerFileSuffix(String path) {

		if (player_event != null) {
			
			if (path != null) {
				player_event.suffix=path.substring(path.lastIndexOf(".")+1,path.length()).toLowerCase(); 
    		}
		}
		
		L.v("Reporter", "logPlayerFileSuffix " + path + "|" + player_event.suffix);
		
		writeLog("logPlayerFileSuffix " + path + "|" + player_event.suffix);
		
	}
	
	public static void logPlayerPause() {
		
		L.v("Reporter", "logPlayerPause");
		
		writeLog( "logPlayerPause");
		
		if (player_event != null) {
			player_event.pause_times++;
		}
		
	}

	public static void logH5Enter(String cid, String ccid, int comeFrom) {
		
		L.v("Reporter", "logH5Enter cid: " + cid + " ccid " + ccid + " comeFrom " + comeFrom);
		
		writeLog("logH5Enter cid: " + cid + " ccid " + ccid + " comeFrom " + comeFrom);
		
		h5_evnt = new Class_d();
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");       
    	Date curDate = new Date(System.currentTimeMillis());
    	
    	h5_evnt.entry_time = formatter.format(curDate);
    	h5_evnt.cid = cid;
    	h5_evnt.ccid = ccid;
    	h5_evnt.comeFrom = comeFrom;
    	
	}
	
	public static void logH5Exit() {
		
		L.v("Reporter", "logH5Exit");
		
		writeLog( "logH5Exit");
		
		if (h5_evnt != null) {
			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
			Date curDate = new Date(System.currentTimeMillis());

			h5_evnt.exit_time = formatter.format(curDate);

			saveReplocal(h5_evnt.toReportString());
			
			h5_evnt = null;
		}
	}
	
	
	private static void savePlayrep(String inf) {
		
		if (!StringUtil.isEmpty(inf)) {
			
			String playrep = getPrefString("playrep_inf");
		
			if (playrep.length() > 0) {
				setPrefString("playrep_inf", playrep + "$#" + inf);
			} else {
				setPrefString("playrep_inf", inf);
			}
		
		}
		
	}
	
	
	private static void saveReplocal(String inf) {

		if (!StringUtil.isEmpty(inf)) {
			
			String replocal = getPrefString("replocal_inf");
			
			Log.v("Reporter", "Reporter replocal_inf: " + replocal + inf);
			
			writeLog("Reporter replocal_inf: " + replocal + inf);
			
			setPrefString("replocal_inf", replocal + inf);

		}
		
	}
	
	private static void cleanReplocal() {
		replocalInf.setLength(0);
		setPrefString("Class_9", "");
		setPrefString("Class_a", "");
		setPrefString("replocal_inf", "");
		
	}
	
	private static void cleanPlayrep() {
		playrepInf.setLength(0);
		setPrefString("playrep_inf", "");
	}

	public static void start() {
		
		Log.v("Reporter", "Reporter start()");
		
		writeLog("Reporter start()");
		
		String inf = getPrefString("replocal_inf");

		if (!StringUtil.isEmpty(inf)) {
			
			Log.v("Reporter", "Reporter replocal_inf: " + inf);
			
			writeLog("Reporter replocal_inf: " + inf);
			
			replocalInf.append(inf);

		}

		inf = getPrefString("Class_9");
		
		if (!StringUtil.isEmpty(inf)) {
			Log.v("Reporter", "Reporter Class_9: " + inf);
			
			writeLog("Reporter Class_9: " + inf);
			replocalInf.append(inf);

		}
		
		inf = getPrefString("Class_a");
		
		if (!StringUtil.isEmpty(inf)) {
			
			Log.v("Reporter", "Reporter Class_a: " + inf);
			
			writeLog("Reporter Class_a: " + inf);
			replocalInf.append(inf);
			
		}

		
		inf = getPrefString("playrep_inf");
		
		playrepInf.append(inf);
		
		report();
	}

	
	private static void report() {

		if (replocalInf.length() > 0) {
			
			Log.v("Reporter", "Reporter report() " + replocalInf);

			writeLog("Reporter report() replocal " + replocalInf);

			try {
				String inf;
				inf = new String(Base64.encode(
						replocalInf.toString().getBytes("UTF-8"), Base64.URL_SAFE
								| Base64.NO_WRAP), "UTF-8");

				Request.getInstance().replocal(inf, "1", new Callback<Rst>() {

					@Override
					public void onSuccess(Rst t) {
						
						if (t.result == 0) {
							cleanReplocal();
						}

						writeLog("Reporter report() replocal Success result: " + t.result);
					}

					@Override
					public void onFailure(Error error) {
						writeLog("Reporter report() replocal Failure " + error);
					}

				});
				
			} catch (UnsupportedEncodingException e) {
			
				e.printStackTrace();
			}

		}

		if (playrepInf.length() > 0) {
			
			Log.v("Reporter", "Reporter report() playrep " + playrepInf);

			writeLog("Reporter report() playrep " + playrepInf);

			try {
				String inf;
				inf = new String(Base64.encode(
						playrepInf.toString().getBytes("UTF-8"), Base64.URL_SAFE
								| Base64.NO_WRAP), "UTF-8");

				Request.getInstance().playrep(inf, "1", new Callback<Rst>() {

					@Override
					public void onSuccess(Rst t) {
						
						if (t.result == 0) {
							cleanPlayrep();
						}

						writeLog("Reporter report() playrep Success result: " + t.result);

					}

					@Override
					public void onFailure(Error error) {
						writeLog("Reporter report() playrep Failure " + error);

					}

				});
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

		}

	}
	
	
	
	public static synchronized void writeLog(String log) {

		if (TextUtils.isEmpty(log) || L.mode_for_release) {
			return;
		}
		
		L.i("report", "writeLog", log);

		String pathDir = Environment.getExternalStorageDirectory().getPath()
				+ "/100tv/Reporter_log";

		File dir = new File(pathDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File file = new File(dir, android.os.Build.MODEL + ".log");

		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				L.e("report", "writeLog", e.getMessage());
			}
		}

		FileWriter fw = null;
		BufferedWriter bw = null;
		
		SimpleDateFormat df = new SimpleDateFormat("MM/dd HH:mm:ss");
		
		try {
			fw = new FileWriter(file, true);
			bw = new BufferedWriter(fw);
			bw.newLine();
			bw.write(df.format(new Date())+ ": "+ log + "\r\n");
			bw.flush();
		} catch (Exception e) {
			L.e("report", "writeLog", e.getMessage());
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
			} catch (Exception e) {
				L.e("report", "writeLog", e.getMessage());
			}
		}
	}
	
	private static SharedPreferences mSharedPreferences;
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public static void setPrefString(String key, String value) {
		
		if (mSharedPreferences == null) {
		
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				mSharedPreferences = ApplicationManage.getAplicationManageInstance().getApplicationContext().getSharedPreferences(
					"Reporter_Info", Context.MODE_MULTI_PROCESS);
			
			} else {
				mSharedPreferences = ApplicationManage.getAplicationManageInstance().getApplicationContext().getSharedPreferences(
						"Reporter_Info", Context.MODE_PRIVATE);
			}
		}
		
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString(key, value);
		editor.commit();
		
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public static String getPrefString(String key) {
		
		if (mSharedPreferences == null) {
			
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
				mSharedPreferences = ApplicationManage.getAplicationManageInstance().getApplicationContext().getSharedPreferences(
					"Reporter_Info", Context.MODE_MULTI_PROCESS);
			
			} else {
				mSharedPreferences = ApplicationManage.getAplicationManageInstance().getApplicationContext().getSharedPreferences(
						"Reporter_Info", Context.MODE_PRIVATE);
			}
		}
		
		return mSharedPreferences.getString(key, "");
		
	}

}
